﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using TimersXP.CommandBase;

namespace TimersXP.ViewModels
{
	/// <summary>This is the host control window for the Timers Host View Model, ViewModelBase gives us the ability of message passing for OnPropertyChanged.</summary>
    public class TimersHost : ViewModelBase
    {
        #region Class Variables
        /// <summary>The timers core</summary>
        private ModelCore modelCore = ModelCore.GetModelCore();
        /// <summary>The list of available skins</summary>
        private ObservableCollection<SkinItem> skins;
        /// <summary>The top most checked</summary>
        private bool topMostChecked;
        #endregion Class Variables

        /// <summary>Gets the timers host context menu click command.</summary>
        /// <value>The timers host context menu click command.</value>
        public RelayCommand<object> TimersHostContextMenuClickCommand { get; private set; }
        /// <summary>Gets the timers host top most click command.</summary>
        /// <value>The timers host top most click command.</value>
        public RelayCommand<object> TimersHostTopMostClickCommand { get; private set; }

        #region Properties
        /// <summary>Gets the list of skins.</summary>
        /// <value>The skins.</value>
        public ObservableCollection<SkinItem> Skins { get; private set; }
        /// <summary>Gets a value indicating whether [top most checked].</summary>
        /// <value><c>true</c> if [top most checked]; otherwise, <c>false</c>.</value>
        public bool TopMostChecked 
        {
            get { return this.topMostChecked; }
            set { this.topMostChecked = value; }
        }
        #endregion Properties

        #region Constructors
        /// <summary>Initializes a new instance of the <see cref="TimersHost"/> class.</summary>
        public TimersHost()
        {
            TimersHostContextMenuClickCommand = new RelayCommand<object>((e) => OnContextMenuItemClick(e));
            TimersHostTopMostClickCommand = new RelayCommand<object>((e) => OnTopMostCheckedChanged(e));

            this.Skins = modelCore.SkinsLibrary.LoadedSkins;
            // Load the default skin.
            this.ApplySkinFromMenuItem(this.Skins[2]);
        }
        #endregion Constructors

        #region Public Functions
        /// <summary>Called when [context menu item click].</summary>
        /// <param name="selectedItem">The selected item.</param>
        private void OnContextMenuItemClick(object selectedItem)
        {
            // Find out which skin was selected from the menu.
            // NOTE: This is a rather NOT effient way of finding the currently selected skin! We shouldn't have to look through all the skins to find the one that was just selected,
            // or if we do, it should be a much faster search than this. As mentioned in this article on Stackoverflow.com:
            // http://stackoverflow.com/questions/10498325/find-item-in-observablecollection-without-using-a-loop
            foreach(SkinItem selectedSkin in this.Skins)
            {
                if (selectedSkin.SkinName == selectedItem)
                {
                    this.ApplySkinFromMenuItem(selectedSkin);
                }
            }
        }

        /// <summary>Called when [top most checked changed].</summary>
        /// <param name="checkBoxStatus">The check box status <c>True</c> If checked, <c>False</c> if unchecked.</param>
		/// <NOTE>When we enter this function topMostChecked contains the new value.</NOTE>
        private void OnTopMostCheckedChanged(object checkBoxStatus)
        {
            this.topMostChecked = ((TimersHost)checkBoxStatus).topMostChecked; //Toggle the status
            if (this.topMostChecked == true)
                modelCore.TimersHostWindow.Topmost = true;
            else
                modelCore.TimersHostWindow.Topmost = false;
        }

        /// <summary>Applies the skin from menu item.</summary>
        /// <param name="skin">The skin.</param>
        public void ApplySkinFromMenuItem(SkinItem skin)
        {
            // Get a relative path to the ResourceDictionary which contains the selected skin.
            string skinPath = skin.SkinPath;
            Uri skinDictUri = new Uri(skinPath, UriKind.Relative);

            // Tell the Application to load the skin resources.
            App app = Application.Current as App;
            //app.ApplySkin(skinDictUri);
            app.ApplySkin(skin.SkinName, skin.BaseSkinName, skin.SkinPath);
            //app.ApplySkin(skin.SkinPath, true);
        }
        #endregion Public Functions
    }
}
